define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hd/forum/info/index' + location.search,
                    add_url: 'hd/forum/info/add',
                    edit_url: 'hd/forum/info/edit',
                    del_url: 'hd/forum/info/del',
                    multi_url: 'hd/forum/info/multi',
                    import_url: 'hd/forum/info/import',
                    table: 'hd_forum_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.name, },
                        { field: 'type.name', title: __('Type_id') },
                        { field: 'user.nickname', title: __('User_id') },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'see_num', title: __('See_num') },
                        {
                            field: 'zan_num',
                            title: __('点赞数量'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'zan_num',
                                    text: function (row) {
                                        if (row.zan_num) {
                                            return row.zan_num
                                        }
                                        return "0"
                                    },
                                    title: __('活动点赞人员记录'),
                                    classname: 'btn btn-xs btn-dialog',
                                    // classname: 'btn btn-xs btn-addtabs',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'hd/forum/likes?in_id={id}',
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'replies',
                            title: __('Replies'),
                            operate: 'LIKE',
                            table: table, class: 'autocontent',
                            buttons: [
                                {
                                    name: 'replies',
                                    text: function (row) {
                                        if (row.replies) {
                                            return row.replies
                                        }
                                        return "0"
                                    },
                                    title: __('活动回复人员记录'),
                                    classname: 'btn btn-xs btn-dialog',
                                    // classname: 'btn btn-xs btn-addtabs',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'hd/forum/posts?in_id={id}',
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        { field: 'is_pinned', title: __('Is_pinned'), formatter: Table.api.formatter.toggle, visible: false },
                        { field: 'is_locked', title: __('Is_locked'), formatter: Table.api.formatter.toggle, visible: false },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                name: function (value, row, index) {
                    const fullText = value; // 完整内容
                    const renderedText = fullText.length > 10 ? fullText.slice(0, 10) + '...' : fullText; // 截断内容
                    return `<a class="label" style="color: #000;font-weight: 500; font-size: 12px;" title="${fullText}">${renderedText}</a>`;
                }
            },
        }
    };
    return Controller;
});
